﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace CheemsControl.Converters.Common
{
    public class BrushToStringConverter : IValueConverter
    {
        // Brush转string
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Brush brush)
            {
                return brush.ToString();
            }
            return string.Empty;
        }

        // string转Brush
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string colorString && !string.IsNullOrEmpty(colorString))
            {
                try
                {
                    return (Brush)new BrushConverter().ConvertFromString(colorString);
                }
                catch
                {
                    return Brushes.Transparent;
                }
            }
            return Brushes.Transparent;
        }
    }
}
